package day14;

import java.io.*;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class User implements Serializable {
    private String phone;
    private String name;
    private String password;
    private Package pack;
    private double consumerMoney;
    private double balance;
    private int realTell;
    private int realCount;
    private int realFlow;

    public User(String phone, String name, String password, Package pack, double consumerMoney, double balance) {
        this.phone = phone;
        this.name = name;
        this.password = password;
        this.pack = pack;
        this.consumerMoney = consumerMoney;
        this.balance = balance;
    }
    public void print(){
        DecimalFormat df = new DecimalFormat("#.##");//保留小数点两位
        System.out.println("注册成功,您的手机号:"+phone+"姓名"+name);
        System.out.println("消费金额："+consumerMoney+"，当前余额："+df.format(balance));
        pack.print();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Package getPack() {
        return pack;
    }

    public void setPack(Package pack) {
        this.pack = pack;
    }

    public double getConsumerMoney() {
        return consumerMoney;
    }

    public void setConsumerMoney(double consumerMoney) {
        this.consumerMoney = consumerMoney;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public int getRealTell() {
        return realTell;
    }

    public void setRealTell(int realTell) {
        this.realTell = realTell;
    }

    public int getRealCount() {
        return realCount;
    }

    public void setRealCount(int realCount) {
        this.realCount = realCount;
    }

    public int getRealFlow() {
        return realFlow;
    }

    public void setRealFlow(int realFlow) {
        this.realFlow = realFlow;
    }
}

interface CallService {
    public void call();
}

interface SendService {
    public void send();
}

interface NetService {
    public void net();
}


abstract class Package implements Serializable{
    double price;

    public abstract void print();

}

class TellPackage extends Package {
    public TellPackage(double price) {
        super.price = price;
    }

    @Override
    public void print() {
        System.out.println("我是话痨套餐，价格：" + price);
    }
}

class SuperPackage extends Package {
    public SuperPackage(double price) {
        super.price = price;
    }

    @Override
    public void print() {
        System.out.println("我是超人套餐，价格：" + price);
    }
}

class NetPackage extends Package {
    public NetPackage(double price) {
        super.price = price;
    }

    @Override
    public void print() {
        System.out.println("我是网虫套餐，价格：" + price);
    }
}

class TestUser {
    //定义一个保存用户信息的集合
    static Map<String, User> map = new HashMap<>();
    static Scanner sc = new Scanner(System.in);
    static {
        input();
    }
    public static void main(String[] args) {
        one();
    }

    public static void one() {//一级菜单
        while (true) {
            System.out.println("XXXX用户系统:");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.println("请输入你的选择");
            int n = sc.nextInt();
            switch (n) {
                case 1:
                    login();
                    break;
                case 2:
                    reg();
                    break;
                case 3:
                    return;
            }
        }
    }

    public static void login() {
        System.out.println("登录功能：先遍历下前面加载好的map集合");
        for (String key:map.keySet()){
            User value = map.get(key);
            value.print();
        }
    }

    public static void reg() {
        String[] phones = randPhone();
        System.out.println("请选择你需要的号码");
        for (int i = 0; i < phones.length; i++) {
            System.out.print(i + 1 + "." + phones[i] + " ");
            if ((i + 1) % 3 == 0) System.out.println();
        }
        System.out.println("请输入编号1-9");
        int n = sc.nextInt();
        String phone = phones[n - 1];
        System.out.println("请输入你要选择的套餐：");
        System.out.println("1.话痨套餐 2.超人套餐 3.网虫套餐");
        int m = sc.nextInt();
        Package p = null;
        switch (m) {
            case 1:
                p = new TellPackage(19.9);break;
            case 2:
                p = new SuperPackage(29.9);break;
            case 3:
                p = new NetPackage(9.9);break;
        }
        Package page = p;
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        System.out.println("请输入预存的话费");
        double money = 0;
        while (true) {
            money = sc.nextDouble();
            if (money < p.price) {
                System.out.println("预存的话费不足以支付这个套餐，请重新充值");
            } else {
                break;
            }
        }
        User user = new User(phone, name, password, p, p.price, money - p.price);
        user.print();
        map.put(phone, user);
        //序列化
        output();
    }
    public static void input(){//反序列化
        try {
            FileInputStream fis = new FileInputStream("d://io//map.user");
            ObjectInputStream ois = new ObjectInputStream(fis);
            map = (Map<String, User>) ois.readObject();
            System.out.println("用户信息获取成功");
        }catch (FileNotFoundException e){
            System.out.println("第一次使用");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void output(){//序列化
        File f = new File("d://io//map.user");
        if (!f.getParentFile().exists()){
            f.getParentFile().mkdirs();
        }
        try {
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            System.out.println("用户信息保存成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String[] randPhone() {
        Random r = new Random();
        String[] phones = new String[9];
        for (int i = 0; i < phones.length; i++) {
            StringBuilder sb = new StringBuilder("139");
            for (int j = 0; j < 8; j++) {
                sb.append(r.nextInt(10));
            }
            phones[i] = sb.toString();
        }
        return phones;
    }
}